<template>
	<view class="">
		<!-- 顶部导航 -->
		<fa-navbar title="申请认证"></fa-navbar>
		<view class="message" v-if="memo && status == 'rejected'">拒绝原因：{{memo}}</view>
		<view class="message hidden" v-if="status == 'hidden'">正在审核中，请耐心等待</view>
		
		<view class="u-p-30">
			<view class="">
				<u-form :model="form" :rules="rules" ref="uForm">
					<u-form-item label-position="top" prop="name" label="姓名">
						<u-input :disabled="disabled || disableds" v-model="form.name" placeholder="请输入您的真实姓名" />
					</u-form-item>
					<u-form-item label-position="top" prop="id_no" label="身份证号码">
						<u-input :disabled="disabled || disableds" v-model="form.id_no" placeholder="请输入您的身份证号码" maxlength="18"/>
					</u-form-item>
					<u-form-item label-position="top" prop="ability" label="技能：(你比较善长于的)">
						<u-input :disabled="disableds" v-model="form.ability" placeholder="例如：内科/外科/心脏病" />
					</u-form-item>
					<u-form-item label-position="top" prop="intro" label="个人介绍：(将会显示在头像右侧)">
						<u-input :disabled="disableds" v-model="form.intro" type="textarea" placeholder="建议不超过10个汉字" />
					</u-form-item>
					<u-form-item label-position="top" prop="mobile" label="手机号码：(用于应急联系)"><u-input v-model="form.mobile" :disabled="disableds" placeholder="请输入你的手机号码" maxlength="11"/></u-form-item>
					<u-form-item label-position="top" prop="aptitude" label="上传资质">
						<u-upload ref="uUpload" :action="action" :auto-upload="true" :form-data="formData" :file-list="aptitude" :show-progress="false"></u-upload>
					</u-form-item>
				</u-form>
			</view>
			<view class="u-p-30 u-m-t-30 u-text-center" v-if="status != 'agreed'">
				<u-button :disabled="disableds" hover-class="none" type="primary" :custom-style="{ backgroundColor: `${theme.bgColor}`, color: `${theme.color}` }" @click="commit">{{memo ? '重新提交' : '提交申请'}}</u-button>
			</view>
		</view>
		<!-- 底部导航 -->
		<fa-tabbar></fa-tabbar>
	</view>
</template>

<script>
export default {
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	data() {
		return {
			disabled: false,
			disableds: false,
			status: null,
			form: {
				name:'',
				id_no:'',
				mobile: '',
				ability: '',
				intro: '',
				aptitude:''
			},
			aptitude:[],
			memo:null,
			formData:{},
			action:'',
			rules: {
				name: [
					{
						required: true,
						message: '请输入真实姓名',
						trigger: ['blur', 'change']
					}
				],
				id_no: [
					{
						required: true,
						message: '请输入身份证号码',
						trigger: ['blur', 'change']
					}
				],
				ability: [
					{
						required: true,
						message: '请输入技能',
						trigger: ['blur', 'change']
					}
				],
				intro: [
					{
						required: true,
						message: '请输入个人介绍',
						trigger: ['blur', 'change']
					}
				],
				mobile: [
					{
						required: true,
						message: '请输入手机号码',
						trigger: ['blur', 'change']
					}
				],
			}
		};
	},
	onLoad() {
		uni.request({
			url: '/wanlshop/common/uploadData',
			success: updata => {
				this.action = updata.data.uploadurl
				this.formData = updata.data.multipart
			}
		})
	},
	async onShow() {
		let res = await this.$api.getExpertGet();
		if(res.data) {
			this.form.name = res.data.name;
			this.form.id_no = res.data.id_no;
			this.form.mobile = res.data.mobile;
			this.form.ability = res.data.ability;
			this.form.intro = res.data.intro;
			this.form.aptitude = res.data.aptitude
			this.status = res.data.status;
			if(res.data.status == 'hidden' || res.data.status == 'agreed') {
				this.disableds = true;
			} else {
				this.disableds = false;
			}
			if(res.data.status == 'rejected' || res.data.status == 'hidden'){
				this.memo = res.data.memo
			} else {
				this.memo = null
			}
			let aptitude = []
			res.data.aptitude.split(',').forEach((item) => {
				aptitude.push({
					url: this.$wanlshop.oss(item)
				})
			})
			this.aptitude = aptitude
		}
		let data = await uni.request({url: '/wanlshop/User/verifiedInfo',method: 'POST'})
		let verifiedInfo = data[1].data
		if(!verifiedInfo) return false;
		if(verifiedInfo.status == 1) {
			this.disabled = true
		} else {
			this.disabled = false
		}
		this.form.name = verifiedInfo.truename;
		this.form.id_no = verifiedInfo.idcard;
	},
	methods: {
		commit() {
			let files = [];
			files = this.$refs.uUpload.lists.map(val => {
				if(val.response) {
					return val.progress == 100 && val.response.data.url
				} else {
					return val.progress == 100 && val.url
				}
			})
			this.form.aptitude = files.join(',');
			this.$refs.uForm.validate(async valid => {
				if (valid) {
					let res = await this.$api.getExpertCreate(this.form);
					this.$u.toast(res.msg);
					if (!res.code) {
						return;
					}
					setTimeout(()=>{
						this.$Router.back(1);
					},800)
				} else {
					console.log('验证失败');
				}
			});
		}
	}
};
</script>

<style>
	.message {
		background-color: #fef0f0;
		color: #fa3534;
		padding: 10rpx;
		font-size: 28rpx;
	}
	.message.hidden {
		color: #19be6b;
		background-color: #dbf1e1;
		text-align: center;
	}
</style>
